package org.ezplatform.office.rd.mszb.web.rest;

import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 秘书转办，领导补办类
 * @author
 * @date 2023/06/30
 */
@RestController
@RequestMapping(value = "/api/w/rd/transferoffice")
public class TransferOfficeController {
    private static Logger logger = Logger.getLogger(TransferOfficeController.class.getName());
    @Resource
    private JdbcTemplate jdbcTemplate;

    @ApiOperation(value = "保存所有秘书的id，为下一步办理的时候流程记录显示做准备", notes = "保存所有秘书的id，为下一步办理的时候流程记录显示做准备")
    @RequestMapping(value = {"/saveMiShuIds"}, method = {RequestMethod.POST})
    public void saveMiShuIds(String dataId,String finMishuIds,String mishuId,String category) {
        String leader_id = myTransferFlowLeaderId(mishuId);
        //判断是发文还是收文
        if(category.equals("goa_doc_sendfile")){
            String sql="update goa_doc_sendfile set msyhid=? where id=?  ";
            jdbcTemplate.update(sql,new Object[] { finMishuIds, dataId});
        }else if(category.equals("goa_doc_receivefile")){
            String sql="update goa_doc_receivefile set msyhid=? where id=?  ";
            jdbcTemplate.update(sql,new Object[] { finMishuIds, dataId});
        }
        UUID uuid = UUID.randomUUID();
        String uuid32 = uuid.toString().replace("-", "").toLowerCase();
        String sql1="INSERT INTO ldbbwjids (\"createdorg\",\"id\",\"createdby\",\"createddate\",\"lastmodifiedby\",\"lastmodifieddate\",\"lastmodifiedorg\",\"optlock\",\"fkid\",\"corp_id\",\"formid\",\"sort_no\",\"ldyhid\",\"gwdataid\",\"gwfl\",\"bbzt\") VALUES\n" +
                "\t ('','"+uuid32+"','',NULL,'',NULL,'',0,'','0','',0,'"+leader_id+"','"+dataId+"','"+category+"','1');  ";
        jdbcTemplate.update(sql1);
    }

    //根据秘书得id获取领导得id
    public String myTransferFlowLeaderId(String userId) {
        String sql = "SELECT scopevalue leader_id ,name leader_name,su.login_name FROM ldgx_scope LEFT  JOIN sys_user su ON su.id=scopevalue WHERE \"fielddatavalue\"=(SELECT f1 FROM ldgx WHERE f2 in(SELECT fielddatavalue FROM ldgx_scope WHERE \"scopevalue\"=?)) ";
        List<Map<String, Object>> rlist=jdbcTemplate.queryForList(sql,new Object[]{userId});
        String leader_id ="";
        if(rlist.size()>0){
            for(int a=0;a<rlist.size();a++) {
                Map amap = (Map) rlist.get(a);
                leader_id = amap.get("leader_id") + "";
            }
        }
        return leader_id;
    }




}
